package com.sfh.cp.user;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.sfh.cp.R;
import com.sfh.cp.base.AbstractBaseFragment;
import com.sfh.cp.mass.MassUserActivity;
import com.sfh.cp.user.login.UserLoginActivity;
import com.sfh.cp.user.pst.IPstUser;
import com.sfh.cp.widget.WidgetBaseToolbar;
import com.sfh.cp.widget.WidgetLabelLayout;
import com.sfh.lib.mvp.IPresenter;
import com.sfh.lib.mvp.service.LoaderService;
import com.squareup.picasso.Picasso;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import crm.sfh.server.entity.EUser;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2018/3/7.
 */

public class UserFragment extends AbstractBaseFragment implements IPstUser.IVUser {


    EUser user;
    @BindView(R.id.toolbar)
    WidgetBaseToolbar toolbar;
    @BindView(R.id.ivHead)
    CircleImageView ivHead;
    @BindView(R.id.tvName)
    TextView tvName;
    @BindView(R.id.lbLyCharm)
    WidgetLabelLayout lbLyCharm;
    @BindView(R.id.lbLyWealth)
    WidgetLabelLayout lbLyWealth;



    @Override
    public int getLayout() {
        return R.layout.user_info_fragment;
    }

    @Override
    public void initData(View view) {
        super.initData(view);
        ButterKnife.bind(this, view);
        toolbar.setTitle("我的");
    }

    @Override
    public void onResume() {
        super.onResume();
        pstUser.getUser();
    }

    @OnClick({R.id.ivHead, R.id.tvSign, R.id.tvOpic, R.id.tvGift, R.id.tvSet})
    public void onViewClicked(View view) {
        if (this.user == null) {
            showToast("请登录");
            UserLoginActivity.startUI(getActivity());
            return;
        }
        switch (view.getId()) {
            case R.id.ivHead:
                UserActivity.startUI(getContext());
                break;
            case R.id.tvSign:
                pstUser.sign();
                break;
            case R.id.tvOpic:
                MassUserActivity.startUI(getActivity(),this.user.getUserId());
                break;
            case R.id.tvGift:
                break;
            case R.id.tvSet:
                break;
            default:
                break;
        }
    }

    IPstUser pstUser;

    @Override
    public IPresenter getPresenter() {
        if (pstUser == null) {
            pstUser = LoaderService.getService(IPstUser.class);
        }
        return pstUser;
    }

    @Override
    public void onUser(EUser user) {
        this.user = user;
        if (this.user == null) {
            return;
        }
        tvName.setText(user.getName());
        lbLyWealth.setStyleWealth(user.getWealth() + "");
        lbLyCharm.setStyleCharm(user.getCharm() + "");
        if (TextUtils.isEmpty(user.getHead())) {
            ivHead.setImageResource(R.mipmap.userimg_default);
        } else {
            Picasso.with(getActivity()).load(user.getHead()).into(ivHead);
        }
    }

}
